use rbatis::plugin::page::PageRequest;
use redis::{Commands, RedisResult};
use uuid::Uuid;
use crate::model::account::Account;
use crate::model::bank::Bank;
use crate::{EXECUTOR, RBATIS};
use crate::utils::redis::get_connection;

pub fn test_string() {
    let mut conn = get_connection();

    let key = String::from("key_1");
    let value = Uuid::new_v4().simple().to_string();

    let set_result: RedisResult<String> = conn.set(&key, &value);
    log::debug!("set {} {} -> {:?}", &key, &value, set_result.unwrap());

    let redis_result: RedisResult<Option<String>> = conn.get(&key);
    if let Ok(Some(val)) = redis_result {
        log::debug!("get {} -> {}", &key, &val);
    } else {
        log::debug!("key【{}】不存在！", &key);
    }
    // redis - rs 库会自动处理连接的关闭操作
    // drop(conn) // 明确关闭连接
}

pub fn test_hash() {
    let mut conn = get_connection();

    let h_key = String::from("h_key_2");
    let sub_key = String::from("sub_key_1");
    let value = Uuid::new_v4().simple().to_string();

    let set_result: RedisResult<String> = conn.hset(&h_key, &sub_key, &value);
    log::debug!("hset {} {} {} -> {:?}", &h_key, &sub_key, &value, set_result.unwrap());

    let redis_result: RedisResult<Option<String>> = conn.hget(&h_key, &sub_key);
    if let Ok(Some(val)) = redis_result {
        log::debug!("hget {} {} -> {}", &h_key, &sub_key, &val);
    } else {
        log::debug!("key【{} {}】不存在！", &h_key, &sub_key);
    }
}

pub fn test_zsort() {
    let mut conn = get_connection();

    let z_key = String::from("z_key_3");
    let member = String::from("sub_key_1");
    let score: f64 = 20241231235959f64;
    let min_score: f64 = 0f64;
    let max_score: f64 = 99991231235959f64;

    let set_result: RedisResult<String> = conn.zadd(&z_key, &member, &score);
    log::debug!("zadd {} {} {} -> {:?}", &z_key, &member, &score, set_result.unwrap());

    let redis_result: RedisResult<Vec<(String, f64)>> = conn.zrangebyscore_withscores(&z_key, &min_score, &max_score);
    // let redis_result: RedisResult<Vec<String>> = conn.zrangebyscore(&z_key, &min_score, &max_score);
    // let redis_result: RedisResult<Vec<(String, f64)>> = conn.zrange_withscores(&z_key, 0, -1);
    if let Ok(val) = redis_result {
        log::debug!("hget {} {} -> {:?}", &z_key, &member, &val);
        // for sub in val {
        //     println!("{} -> {}", sub.0, sub.1);
        // }
    } else {
        log::debug!("key【{} {}】不存在！", &z_key, &member);
    }
}

async fn test_crud() {
    let accounts = Account::select_all(&RBATIS.clone()).await.unwrap(); // 此处线程名称改变
    log::debug!("select_all -> {:?}", accounts);

    let banks = Bank::select_by_column(&RBATIS.clone(), "resourceId", "1").await.unwrap();
    log::debug!("select_by_column -> {:?}", banks);
}

async fn test_macros() {
    let bank = Bank::select_by_id(&RBATIS.clone(), "2").await.unwrap();
    log::debug!("select_by_id -> {:?}", bank);

    let page_request = PageRequest::new(1, 10);
    let bank = Bank::select_page(&RBATIS.clone(), &page_request, "2").await.unwrap();
    log::debug!("select_page -> {:?}", bank);

    let data = Bank::select_page_data(&RBATIS, &page_request, "6217").await;
    log::debug!("select_page_data -> {:?}", data);

    let page_request = PageRequest::new(1, 10).set_do_count(false);
    let page = Bank::select_page_by_htmlsql(&RBATIS, &page_request, "6217").await;
    log::debug!("select_page_by_htmlsql -> {:?}", page);
}

async fn test_custom_sql() {
    let bank = Bank::test_by_sql(&RBATIS, String::from("62170000")).await.unwrap();
    log::debug!("test_by_sql -> {:?}", bank);

    let bank = Bank::test_custom_page(&RBATIS, "6217", 0, 10).await.unwrap();
    log::debug!("test_custom_page -> {:?}", bank);

    let page = Bank::select_custom_html_sql(&RBATIS, "6217").await.unwrap();
    log::debug!("select_custom_html_sql -> {:?}", page);
}

pub async fn test_sql() {
    test_crud().await;
    test_macros().await;
    test_custom_sql().await;
}

pub fn test_sql_sync() {
    log::info!("开始测试异步sql查询...");
    for _i in 0..10 {
        EXECUTOR.spawn(test_crud());
        EXECUTOR.spawn(test_macros());
        EXECUTOR.spawn(test_custom_sql());
    }
}